<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\Car;

/**
 * 价格相关计算
 *
 * @author
 */
class Price
{

    private $car;
    private $owner;
    private $bargain;

    public function __construct()
    {
        $this->car = M("cars");
        $this->owner = M("car_owner");
        $this->bargain = M("bargain_log");
    }

    /**
     * 获取车辆最终成交价
     *
     *  最终成交价 = 拍卖最高价(第一名出价) + 公司补贴 - 车主降价 + 抬价金额
     *
     * @param $orderId 拍单ID
     * @return mixed
     */
    public static function getLastTransactionPrice($orderId)
    {
        if (empty($orderId)) {
            AuctionException::make_throw('700002');
        }
        $res = '';
        //要查询的详情
        $cols = "o.order_id,o.order_no,o.car_id,o.bid_best_price,o.bidding_best_price,o.company_subsidies,
                oi.status as issue_status,oi.cut_down_price,c.bid_up_price";
        $prefix = C('DB_PREFIX');
        $cond = "o.order_id=" . $orderId;
        $res = M('order')->alias('o')->field($cols)
            ->join('LEFT JOIN ' . $prefix . "cars as c on c.car_id = o.car_id")
            ->join('LEFT JOIN ' . $prefix . "owner_issue as oi ON oi.order_id=o.order_id")
            ->where($cond)->find();
        // $firstAuctionPrice = !empty($res['bidding_best_price']) ? $res['bidding_best_price'] : 0;
        if ($res['bidding_best_price'] > $res['bid_best_price']) {
            $firstAuctionPrice = $res['bidding_best_price'];
        } else {
            $firstAuctionPrice = $res['bid_best_price'];
        }
        $companyAllowance = !empty($res['company_subsidies']) ? $res['company_subsidies'] : 0;
        $ownerLower = !empty($res['cut_down_price']) ? $res['cut_down_price'] : 0;
        $bid_up_price = !empty($res['bid_up_price']) ? $res['bid_up_price'] : 0;
        $res = $firstAuctionPrice + $companyAllowance - $ownerLower + $bid_up_price;
        return $res;
    }

    /**
     * 获取第一名出价
     *
     *  第一名出价 = max(竞标阶段最高价(第一名), 投标阶段最高价)
     *
     * @param int $orderId 拍单ID
     * @return mixed
     */
    public static function getBestPrice($orderId)
    {
        if (empty($orderId)) {
            AuctionException::make_throw('700002');
        }
        //要查询的详情
        $cols = "order_id,order_no,car_id,bid_best_price,bidding_best_price,company_subsidies";
        $order = M('order')->field($cols)->where('order_id=' . $orderId)->find();
        return $order['bidding_best_price'] > $order['bid_best_price'] ? $order['bidding_best_price'] : $order['bid_best_price'];
    }
}
